package cn.edu.suda.schoolBus.controller;

import java.sql.Time;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.suda.schoolBus.bean.DynamicCarNumber;
import cn.edu.suda.schoolBus.service.DynamicCarNumberService;
import cn.edu.suda.schoolBus.util.ResultUtil;

@Controller
public class DynamicCarNumberController {

	@Autowired
	private DynamicCarNumberService dynamicCarNumberService;

	private static int i;
	private int num;
	private Time time;
	private List<DynamicCarNumber> dynamicCarNumbers;

	public static void setI(int i) {
		DynamicCarNumberController.i = i;
	}

	private void getDynamicCarNumber() {
		time = null;
		dynamicCarNumbers = dynamicCarNumberService.getDynamicCarNumber(time);
		num = dynamicCarNumbers.size();
	}

	private void compareTime() {
		time = new Time(new Date().getTime() + 60 * 1000);
		for (DynamicCarNumber dynamicCarNumber : dynamicCarNumbers) {
			if (dynamicCarNumber.getCarNumber().getDepartureTime().toString().compareTo(time.toString()) < 0) {
				if (dynamicCarNumber.getDcnStatus() == 0) {
					editDynamicCarNumber(dynamicCarNumber.getDcnId(), 2);
				}
			} else {
				i = dynamicCarNumbers.indexOf(dynamicCarNumber);
				break;
			}
		}
	}

	@RequestMapping("/getCurrentCarNumber")
	@ResponseBody
	public DynamicCarNumber getCurrentCarNumber() {
		getDynamicCarNumber();
		if (num == 0) {
			return null;
		} else {
			compareTime();
			getDynamicCarNumber();
			return dynamicCarNumbers.get(i);
		}
	}

	@RequestMapping("/getLeftCarNumber")
	@ResponseBody
	public DynamicCarNumber getLeftCarNumber() {
		getDynamicCarNumber();
		if (i > 0) {
			i--;
			return dynamicCarNumbers.get(i);
		}
		return null;
	}

	@RequestMapping("/getRightCarNumber")
	@ResponseBody
	public DynamicCarNumber getRightCarNumber() {
		getDynamicCarNumber();
		if (i < num - 1) {
			i++;
			return dynamicCarNumbers.get(i);
		}
		return null;
	}

	@RequestMapping("/editDynamicCarNumber")
	@ResponseBody
	public Map<String, Boolean> editDynamicCarNumber(int dcnId, int status) {
		if (status == 1) {
			new Timer().schedule(new TimerTask() {

				@Override
				public void run() {
					dynamicCarNumberService.editDynamicCarNumber(dcnId, 3);
				}
			}, 60 * 1000);
		}
		return ResultUtil.getResult(dynamicCarNumberService.editDynamicCarNumber(dcnId, status));
	}

	@RequestMapping("/statisticsCarNumber")
	@ResponseBody
	public Map<String, Object> statisticsCarNumber(int days) {
		return dynamicCarNumberService.statisticsCarNumber(days);
	}

	@RequestMapping("/client/getCarNumber")
	@ResponseBody
	public List<DynamicCarNumber> getCarNumber() {
		time = new Time(new Date().getTime());
		return dynamicCarNumberService.getDynamicCarNumber(time);
	}

	@RequestMapping("/client/getDynamicCarNumber")
	@ResponseBody
	public Map<String, Boolean> getCarNumber(int dcnId) {
		int i = 0;
		if (dynamicCarNumberService.getDynamicCarNumber(dcnId) != null) {
			if (dynamicCarNumberService.getDynamicCarNumber(dcnId).getDcnStatus() == 0) {
				i = 1;
			}
		}
		return ResultUtil.getResult(i);
	}

	@RequestMapping("/client/updateDynamicCarNumber")
	@ResponseBody
	public Map<String, Boolean> updateCarNumber(int dcnId, int num) {
		return ResultUtil.getResult(dynamicCarNumberService.updateDynamicCarNumber(dcnId, num));
	}

}
